package com.homegoods.login.model;

import android.content.Context;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.android.databinding.library.baseAdapters.BR;
import com.homegoods.login.evnet.LoginOutEvent;
import com.homegoods.resource.HomeGoodsApp;
import com.kedll.library.util.MyLog;
import com.kedll.library.util.RxBus;
import com.kedll.library.util.SharePreUtils;
import com.network.utils.NetworkUtils;


/**
 * 用户资料操作类
 *
 * @author QiQi
 * @date 2017/5/9
 */

public class UserProxy extends BaseObservable {


    private final Object obj = new Object();
    private UserModel mClientUser;
    private Context mContext;

    private UserProxy() {

    }

    public static UserProxy getInstance() {
        return UserProxyHolder.instance;
    }

    public void init(Context context) {
        mContext = context;
    }

    /**
     * 重置
     */
    public void reset() {
        mClientUser = null;
    }

    @Bindable
    public String getUserID() {
        if (isLogin()) {
            return String.valueOf(mClientUser.getRid());
        }
        return "";
    }

    /**
     * 是否登录
     *
     * @return true = 已登录 false = 未登录
     */
    @Bindable
    public boolean isLogin() {
        return getClientUser() != null;
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @Bindable
    public UserModel getClientUser() {
        synchronized (obj) {
            if (mClientUser != null) {
                return mClientUser;
            }
            String registerAccount = getAutoRegisterAccount();
            if (registerAccount != null) {
                mClientUser = UserModel.parserUserBean(registerAccount);
            }
            return mClientUser;
        }
    }

    /**
     * 从本地回去用户信息
     *
     * @return
     */
    private String getAutoRegisterAccount() {
        if (mContext == null) {
            throw new NullPointerException("请在Application中调用init方法进行初始化");
        }
        String userData = SharePreUtils.getAccount(mContext);
        if (TextUtils.isEmpty(userData)) {
            return null;
        }
        return userData;
    }

    /**
     * 更新并重新保存用户信息
     */
    public void resetClientUser() {
        if (mContext == null) {
            throw new NullPointerException("请在Application中调用init方法进行初始化");
        }
        if (mClientUser == null) {
            SharePreUtils.saveAccount(mContext, "");
        } else {
//            String userBase64 = Base64.encodeToString(mClientUser.toString().getBytes(), 0);
            SharePreUtils.saveAccount(mContext, JSON.toJSONString(mClientUser));
        }
    }


    public void setUserModel(String modelJson) {
        mClientUser = UserModel.parserUserBean(modelJson);
        resetClientUser();
    }

    @Bindable
    public String getMPhone() {
        if (isLogin()) {
            return mClientUser.getMPhone();
        }
        return "";
    }

    @Bindable
    public String getRoleName() {
        if (isLogin()) {
            return mClientUser.getRoleName();
        }
        return "";
    }

    public void setMPhone(String mobile) {
        mClientUser.setMPhone(mobile);
        notifyPropertyChanged(BR.mPhone);
        resetClientUser();
    }

    @Bindable
    public String getPushSign() {
        if (isLogin()) {
            return JSON.toJSONString(mClientUser.getPushSign());
        }
        return "";
    }

    @Bindable
    public String getCapitalVol() {
        if (isLogin()) {
            return mClientUser.getCapitalVol();
        }
        return "";
    }

    public void setCapitalVol(String capitalVol) {
        mClientUser.setCapitalVol(capitalVol);
        notifyPropertyChanged(BR.capitalVol);
        resetClientUser();
    }

    @Bindable
    public String getHeadImg() {
        if (isLogin()) {
            return mClientUser.getSmallPic();
        }
        return "";
    }

    @Bindable
    public String getNickName() {
        if (isLogin()) {
            return mClientUser.getNickName();
        }
        return "";
    }

    public void setNickName(String nickname) {
        mClientUser.setNickName(nickname);
        notifyPropertyChanged(BR.nickName);
        resetClientUser();
    }

//    @Bindable
//    public String getName() {
//        if (isLogin()) {
//            return mClientUser.getLoginName();
//        }
//        return "";
//    }
//
//    public void setName(String name) {
//        mClientUser.setName(name);
//        notifyPropertyChanged(BR.name);
//        resetClientUser();
//    }


    @Bindable
    public String getSex() {
//        if (isLogin()) {
//            if (TextUtils.isEmpty(mClientUser.getSex())) {
//                return "未设置";
//            }
//
//            switch (mClientUser.getSex()) {
//                case "M":
//                    return "男";
//                case "F":
//                    return "女";
//                default:
//                    return "未知";
//            }
//        }
        return "未设置";
    }

    public void setSex(String sex) {
//        mClientUser.set(sex);
        notifyPropertyChanged(BR.sex);
        resetClientUser();
    }

    @Bindable
    public String getArea() {
        if (isLogin()) {
//            return mClientUser.getArea();
        }
        return "";
    }

    public void setArea(String area) {
//        mClientUser.setArea(area);
        notifyPropertyChanged(BR.area);
        resetClientUser();
    }

    @Bindable
    public String getPortraitURL() {
        if (isLogin()) {
//            return mClientUser.getPortraitURL();
        }
        return "";
    }

    public void setPortraitURL(String url) {
//        mClientUser.setPortraitURL(url);
        notifyPropertyChanged(BR.portraitURL);
        resetClientUser();
    }

    public void setStatus(int status) {
//        mClientUser.setState(status);
        resetClientUser();
    }

    @Bindable
    public String getRoles() {
//        if (isLogin()) {
//            if (CollectionUtils.isNotEmpty(mClientUser.getRoles())) {
//                StringBuilder roleNames = new StringBuilder();
//                for (UserModel.RolesBean role : mClientUser.getRoles()) {
//                    roleNames.append(role.getRoleName()).append(",");
//                }
//                return (roleNames.substring(0, roleNames.length() - 1));
//            }
//        }
        return null;
    }

//    public void setRoles(List<UserModel.RolesBean> obj) {
//        mClientUser.setRoles(obj);
//        resetClientUser();
//        notifyPropertyChanged(BR.roles);
//    }

    public boolean isAdmin() {
        if (isLogin()) {
//            return mClientUser.isAdmin();
        }
        return false;
    }

    public void setAdmin(boolean isAdmin) {
//        mClientUser.setAdmin(isAdmin);
        resetClientUser();
    }


    public void loginOut() {
//        ApiUtil.logout(getToken(), aBoolean -> {
//            MyLog.getInstance().dismissDialog();
//            saveClientUser(null);
//            RxBus.getDefault().post(new LoginOutEvent());
////            JPushInterface.clearAllNotifications(mContext);
////            PrintControllerUtil.deleteAlias(mContext);
//        });
        MyLog.getInstance().dismissDialog();
        saveClientUser(null);
        HomeGoodsApp.deleteToken();
        RxBus.getDefault().post(new LoginOutEvent());
    }

    public String getToken() {
        if (isLogin()) {
            return mClientUser.getToken();  //modify shibo.zheng
        }
        return "";
    }

    public String getSid() {
        if (isLogin()) {
            return /*mClientUser.getSid()*/"";
        }
        return "";
    }

    /**
     * 保存注册账号信息
     *
     * @return
     */
    public void saveClientUser(UserModel userBean) {
        mClientUser = userBean;
        resetClientUser();
    }

    private static class UserProxyHolder {
        public static UserProxy instance = new UserProxy();
    }


}
